package com.greenantmall.bean;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.avos.avoscloud.AVACL;
import com.avos.avoscloud.AVClassName;
import com.avos.avoscloud.AVRole;
import com.greenantmall.utils.UtilTools;

@AVClassName(AntAVObject.CLASS_SERVERSECTION)
public class ServerSection extends AntAVObject
{

    public static final String ATTR_USRID = "usrId";
    public static final String ATTR_SECTIONNUM = "sectionNum";//对应ATTR_AREA来区分
    
    public static final String ATTR_UPTOWN = "uptown";// 淮祥园
    public static final String ATTR_STREET = "street";// 姚江东路
    public static final String ATTR_AREA = "area";// 北辰区
    public static final String ATTR_CITY = "city";// 天津
    public static final String ATTR_PROVINCE = "province";// 天津
    public static final String ATTR_PICKUPSTATION = "pickupStation";// 自提点
    public static final String ATTR_SHOWCARTSTOREID = "showCartStoreId";
    public static final String ATTR_OPENTIME = "openTime";
    public static final String ATTR_CLOSETIME = "closeTime";
    public static final String ATTR_NOTICE = "notice";
    public static final String ATTR_CVSID = "cvsId";

    private AVRole customRole;

    public ServerSection()
    {
        super();
        setAccessCtrl();
    }

    public ServerSection(String str)
    {
        super(str);
        setAccessCtrl();
    }
    
    /**
	 * 商店是否开门
	 * @return
	 */
    public boolean isOpen()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
        Date date = new Date();
        date.setTime(System.currentTimeMillis());
        String current = sdf.format(date);
        String open = sdf.format(this.getOpenTime());
        String close = sdf.format(this.getCloseTime());

        double currentTime = Double.parseDouble(current);
        double openTime = Double.parseDouble(open);
        double closeTime = Double.parseDouble(close);

        if (currentTime > openTime && currentTime < closeTime)
        {
            return true;
        }
        return false;
    }
    
    public Date getOpenTime() {
		return UtilTools.parseDateStr("HH:mm", this.getOpenTimeStr());
	}
    
    public String getOpenTimeStr() {
    	String str = this.getString(ATTR_OPENTIME);
    	if(str == null) {
    		return "";
    	}
		return str;
	}

	public void setOpenTimeStr(String openTime) {
		this.put(ATTR_OPENTIME,openTime);
	}
	
	public Date getCloseTime() {
		return UtilTools.parseDateStr("HH:mm", this.getCloseTimeStr());
	}
	
	public String getCloseTimeStr() {
		String str = this.getString(ATTR_CLOSETIME);
    	if(str == null) {
    		return "";
    	}
		return str;
	}

	public void setCloseTimeStr(String closeTime) {
		this.put(ATTR_CLOSETIME, closeTime);
	}

    public int getSectionnum()
    {
    	return this.getInt(ATTR_SECTIONNUM);
    }

    public void setSectionnum(int sectionnum)
    {
        this.put(ATTR_SECTIONNUM, sectionnum);
    }
    
    public String getPickupStation()
    {
        return this.getString(ATTR_PICKUPSTATION);
    }

    public void setPickupStation(String uptown)
    {
        this.put(ATTR_PICKUPSTATION, uptown);
    }
    
    public String getCvsid()
    {
    	String cvsid = this.getString(ATTR_CVSID);
        return (cvsid==null ? "" : cvsid);
    }

    public void setCvsid(String cvsid)
    {
        this.put(ATTR_CVSID, cvsid);
    }
    
    public String getUptown()
    {
        return this.getString(ATTR_UPTOWN);
    }

    public void setUptown(String uptown)
    {
        this.put(ATTR_UPTOWN, uptown);
    }
    
    public String getNotice()
    {
        return this.getString(ATTR_NOTICE);
    }

    public void setNotice(String notice)
    {
        this.put(ATTR_NOTICE, notice);
    }
    
    public String getShowCartStoreId()
    {
        return this.getString(ATTR_SHOWCARTSTOREID);
    }

    public void setShowCartStoreId(String showCartStoreId)
    {
        this.put(ATTR_SHOWCARTSTOREID, showCartStoreId);
    }
    
    public String getStreet()
    {
        return this.getString(ATTR_STREET);
    }

    public void setStreet(String street)
    {
        this.put(ATTR_STREET, street);
    }
    
    public String getArea()
    {
        return this.getString(ATTR_AREA);
    }

    public void setArea(String area)
    {
        this.put(ATTR_AREA, area);
    }
    
    public String getCity()
    {
        return this.getString(ATTR_CITY);
    }

    public void setCity(String city)
    {
        this.put(ATTR_CITY, city);
    }
    
    public String getProvince()
    {
        return this.getString(ATTR_PROVINCE);
    }

    public void setProvince(String province)
    {
        this.put(ATTR_PROVINCE, province);
    }
    
    public ServerSection getInstance()
    {
        return this;
    }

    public String getUserId()
    {
        return this.getString(ATTR_USRID);
    }

    private void setUserId(String userId)
    {
        this.put(ATTR_USRID, userId);
    }
    
	public String getAddress(){
    
    return getCity() + getArea() + getUptown();
	}

    private int setAccessCtrl()
    {

        acl = new AVACL();
        acl.setPublicReadAccess(true);//�˴����õ��������˵Ŀɶ�Ȩ��
//        acl.setReadAccess(AVUser.getCurrentUser(), true);
//        acl.setWriteAccess(AVUser.getCurrentUser(), true);// �������������ļ������ߵ�дȨ��
//        this.setACL(acl);
//        this.setUserId(AVUser.getCurrentUser().getObjectId());
//        AVQuery<AVRole> query = new AVQuery<AVRole>("_Role");
//        query.whereEqualTo("name", "custom");
//        query.findInBackground(new FindCallback<AVRole>()
//        {
//            @Override
//            public void done(List<AVRole> avObjects, AVException e)
//            {
//                if (e == null)
//                {
//                    customRole = (AVRole) avObjects.get(0);
//                    acl.setRoleReadAccess(customRole, true);
//                    getInstance().setACL(acl);
//
//                } else
//                {
//                    Log.d("ʧ��", "��ѯ����: " + e.getMessage());
//                }
//            }
//
//        });
        return 0;
    }

}
